var app = getApp()
Page({
  data: {
    openid: "",
    session_key: "",
    password: "",
    passconf: ""
  },
  ChangePassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  ChangePassconf: function (e) {
    this.setData({
      passconf: e.detail.value
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (that.data.password != "" && that.data.password == that.data.passconf) {
      console.log(e.detail.errMsg)
      console.log(e.detail.iv)
      console.log(e.detail.encryptedData)
      if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '未授权无法注册',
          success: function (res) { }
        })
      } else {
        wx.request({
          url: getApp().globalData.url + 'index.php/User/Login/WechatRegister',
          method: 'POST',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: {
            openid: that.data.openid,
            session_key: that.data.session_key,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            password: that.data.password,
            headimg: ""
          },
          success: function (res) {
            if (res.data == 1) {
              wx.showModal({
                title: '成功',
                showCancel: false,
                content: '注册成功',
                success: function (res) {
                  wx.redirectTo({
                    url: '../index/index',
                  })
                }
              })
            } else {
              wx.showModal({
                title: '失败',
                showCancel: false,
                content: "该手机号已被注册",
                success: function (res) {

                }
              })
            }
          }
        })
      }
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确密码并两次密码相同',
        success: function (res) {

        }
      })
    }
  },
  onLoad: function () {
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      console.log(userInfo);
      that.setData({
        userInfo: userInfo
      })
      wx.login({
        success: function (loginCode) {
          console.log(loginCode);
          wx.request({
            url: getApp().globalData.url + 'index.php/User/Login/GetSessionKey',
            method: "POST",
            data: {
              code: loginCode.code
            },
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              console.log(res);
              that.setData({
                openid: res.data.openid,
                session_key: res.data.session_key
              })
            }
          })
        }
      })
    })
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1];
    var web_src = '';
    if (!currentPage.options.return_url) {
      web_src = getApp().globalData.weburl
    } else {
      web_src = decodeURIComponent(currentPage.options.return_url)
    }
    that.setData({
      title: "智休商城",
      weburl: web_src
    });
  },
  onShareAppMessage: function (options) {
    var that = this;
    var return_url = options.webViewUrl;
    var path = '/pages/index/index?return_url=' + encodeURIComponent(return_url);
    return {
      title: that.data.title,
      path: path
    }
  },

})